package health.lm.com.home.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.fragment.XLazyListFragment;
import health.lm.com.data.HttpCST;
import health.lm.com.home.R;
import health.lm.com.home.R2;
import health.lm.com.home.adapter.NoticesAdapter;
import health.lm.com.home.entity.NoticesEntity;
import health.lm.com.home.presenter.NoticesPresenter;
import health.lm.com.router.ARouterConstant;

/**
 * Created by Android Studio .
 * Project：hlian health.lm.com.home.fragment
 * Date：2019/8/7 16:36
 * Introduce：商会通知
 */
public class NoticesBusinessFragment extends XLazyListFragment<NoticesPresenter> implements NoticesView {
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.srl_layout)
    SmartRefreshLayout mSrlLayout;
    private NoticesAdapter mNoticesAdapter;

    /**
     * 加载数据
     *
     * @param isRefresh true 刷新 false 加载更多
     * @param layout    头部布局
     * @param page      页数
     * @param pageSize  每页数量
     */
    @Override
    public void loadListData(boolean isRefresh, Object layout, int page, int pageSize) {
        getP().getBusinessNotices(isRefresh, page, pageSize);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_notices_huichain;
    }

    @Override
    public NoticesPresenter newP() {
        return new NoticesPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mNoticesAdapter = new NoticesAdapter(new ArrayList<>());
        mRvList.setLayoutManager(new LinearLayoutManager(context));
        mRvList.setAdapter(mNoticesAdapter);
        mNoticesAdapter.setOnItemClickListener((adapter1, view, position) -> {
            NoticesEntity entity = (NoticesEntity) adapter1.getData().get(position);
            ARouter.getInstance().build(ARouterConstant.NoticesDetailActivity)
                    .withInt(HttpCST._id, entity.getId()).navigation();
        });

        super.recyclerView = mRvList;
        super.rlRefreshLayout = mSrlLayout;
        super.adapter = mNoticesAdapter;
        super.initData(savedInstanceState);
        getP().getBusinessNotices(isRefresh, page, pageSize);
    }

    @Override
    public void setNewData(List<NoticesEntity> pNewData) {
        if (isRefresh) {
            mNoticesAdapter.setNewData(pNewData);
            mSrlLayout.finishRefresh();
        } else {
            mNoticesAdapter.addData(pNewData);
            if (pNewData.size() >= pageSize) mNoticesAdapter.loadMoreComplete();
            else mNoticesAdapter.loadMoreEnd();
        }
        if (mNoticesAdapter.getData().size() <= 0) mNoticesAdapter.setEmptyView(empty());
    }
}
